<?php

/**
 * Categoria form base class.
 *
 * @package    form
 * @subpackage categoria
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $
 */
class BaseCategoriaForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id_categoria'     => new sfWidgetFormInputHidden(),
      'nm_categoria'     => new sfWidgetFormInput(),
      'id_categoria_pai' => new sfWidgetFormDoctrineChoice(array('model' => 'Categoria', 'add_empty' => true)),
      'ds_categoria'     => new sfWidgetFormInput(),
      'st_categoria'     => new sfWidgetFormInput(),
      'slug'             => new sfWidgetFormInput(),
      'created_at'       => new sfWidgetFormDateTime(),
      'updated_at'       => new sfWidgetFormDateTime(),
      'deleted'          => new sfWidgetFormInputCheckbox(),
      'item_list'        => new sfWidgetFormDoctrineChoiceMany(array('model' => 'Item')),
      'tipo_list'        => new sfWidgetFormDoctrineChoiceMany(array('model' => 'Tipo')),
    ));

    $this->setValidators(array(
      'id_categoria'     => new sfValidatorDoctrineChoice(array('model' => 'Categoria', 'column' => 'id_categoria', 'required' => false)),
      'nm_categoria'     => new sfValidatorString(array('max_length' => 50)),
      'id_categoria_pai' => new sfValidatorDoctrineChoice(array('model' => 'Categoria', 'required' => false)),
      'ds_categoria'     => new sfValidatorString(array('max_length' => 150, 'required' => false)),
      'st_categoria'     => new sfValidatorInteger(array('required' => false)),
      'slug'             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'created_at'       => new sfValidatorDateTime(array('required' => false)),
      'updated_at'       => new sfValidatorDateTime(array('required' => false)),
      'deleted'          => new sfValidatorBoolean(),
      'item_list'        => new sfValidatorDoctrineChoiceMany(array('model' => 'Item', 'required' => false)),
      'tipo_list'        => new sfValidatorDoctrineChoiceMany(array('model' => 'Tipo', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'Categoria', 'column' => array('slug')))
    );

    $this->widgetSchema->setNameFormat('categoria[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Categoria';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['item_list']))
    {
      $this->setDefault('item_list', $this->object->Item->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['tipo_list']))
    {
      $this->setDefault('tipo_list', $this->object->Tipo->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveItemList($con);
    $this->saveTipoList($con);
  }

  public function saveItemList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['item_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Item->getPrimaryKeys();
    $values = $this->getValue('item_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Item', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Item', array_values($link));
    }
  }

  public function saveTipoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tipo_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Tipo->getPrimaryKeys();
    $values = $this->getValue('tipo_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Tipo', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Tipo', array_values($link));
    }
  }

}
